<template>
  <view class="page">
    <view class="search-bar">
      <view class="search-input">
        <input type="text" v-model="keywords" placeholder="请输入关键字" />
        <image class="icon-search" src="/static/images/icon_search.png" mode=""></image>
        <view class="search-button" @click="search">搜索</view>
      </view>
    </view>
    <scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
      <view class="card-list">
        <view class="card-item" v-for="(item, index) in list" :key="index"
          @click="navTo('/pages/sys/msg/notice-detail?id='+item.id)">
          <view class="card-item-hd">
            <image class="icon" src="/static/images/icon_notice.png" mode=""></image>
            {{item.categoryName}}
          </view>
          <view class="card-item-bd">
            <view class="card-title">{{item.title}}</view>
            <view class="card-content">
              <uni-text>{{getContent(item.content)}}</uni-text>
            </view>
            <view class="card-desc">{{item.releaseTime}}</view>
          </view>
          <view class="card-item-ft">
            查看详情
            <view class="icon-arrow"></view>
          </view>
        </view>
      </view>
      <u-loadmore class="loadmore" :status="loadStatus"></u-loadmore>
    </scroll-view>
  </view>
</template>
<script>
  import $time from '@/common/time.js';
  export default {
    data() {
      return {
        keywords: '',
        query: {
          current: 1,
          size: 10
        },
        list: [],
        pages: 0,
        loadStatus: 'loadmore',
        categoryList: []
      };
    },
    onLoad() {
      this.initCategory();
      this.loadList();
    },
    computed: {

    },
    methods: {
      navTo(url) {
        uni.navigateTo({
          url: url
        });
      },
      formatYear(createTime) {
        return $time.dateFormat(new Date(createTime.replace(/-/g, '/')), "{Y}");
      },
      formatMonthAndDay(createTime) {
        return $time.dateFormat(new Date(createTime.replace(/-/g, '/')), "{MM}-{DD}");
      },
      initCategory() {
        this.$u.api.dictData({
          code: 'notice'
        }).then(res => {
          this.categoryList = res.data;
        });
      },
      search() {
        this.list = [];
        this.query.current = 0;
        this.query.title = this.keywords;
        this.loadList();
      },
      loadList() {
        this.$u.api.notice.list(this.query).then(res => {
          if (!res.data.records || res.data.records.length == 0) {
            this.loadStatus = "nomore";
            return;
          }
          this.list = this.list.concat(res.data.records);
          this.pages = res.pages;
          this.query.current = res.data.current;
          this.query.size = res.data.size;
          this.loadStatus = "loadmore";
        });
      },
      loadMore() {
        this.loadStatus = "loading";
        setTimeout(() => {
          this.query.current += 1;
          this.loadList();
        }, 100);
      },
      getContent(value) {
        let res = value.replace(/<.+?>/g, '').replace(/&nbsp;/ig, '').replace(/\s/ig, '');
        return res;
      },
    }
  };
</script>
<style lang="scss">
  .page {
    background: #F7F7F7;
  }
  
  .search-bar {
    display: flex;
    align-items: center;
    padding: 16rpx 32rpx;
    background: #fff;
  }

  .search-input {
    position: relative;
    width: 100%;
    height: 64rpx;
    border-radius: 32rpx;
    background: #F7F7F7;
    overflow: hidden;

    input {
      width: 100%;
      height: 64rpx;
      border: 0;
      padding: 0 144rpx 0 64rpx;
      outline: 0;
      line-height: 64rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: #999;
      background: transparent;
    }

    .icon-search {
      position: absolute;
      top: 21rpx;
      left: 21rpx;
      width: 22rpx;
      height: 22rpx;
    }

    .search-button {
      position: absolute;
      top: 0;
      right: 0;
      width: 144rpx;
      border-radius: 32rpx;
      line-height: 64rpx;
      text-align: center;
      font-size: 24rpx;
      font-weight: 700;
      color: #fff;
      background: #3989FC;
    }
  }

  .scroll-list {
    height: calc(100vh - var(--window-top) - var(--window-bottom) - 96rpx);

    .loadmore {
      padding: 32rpx;
    }
  }

  .card-list {
    display: flex;
    flex-direction: column;
    padding: 24rpx 24rpx 0;
  }

  .card-item {
    width: 100%;
    border-radius: 8rpx;
    margin-bottom: 24rpx;
    background: #fff;
  }

  .card-item-hd {
    display: flex;
    align-items: center;
    padding: 32rpx;
    font-size: 24rpx;
    font-weight: 500;
    color: #999;

    .icon {
      width: 34rpx;
      height: 34rpx;
      margin-right: 16rpx;
    }
  }

  .card-item-bd {
    padding: 0 32rpx 32rpx;
    line-height: 42rpx;
    font-size: 24rpx;
    font-weight: 500;
    color: #999;
  }

  .card-title {
    margin-bottom: 32rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 32rpx;
    font-weight: 700;
    color: #000;
    overflow: hidden;
  }

  .card-content {
    height: 83rpx;
    overflow: hidden;

    text {
      display: -webkit-box;
      word-break: break-all;
      overflow: hidden;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

  .card-desc {
    margin-top: 8rpx;
    color: #ccc;
  }

  .card-item-ft {
    position: relative;
    padding: 32rpx;
    font-size: 28rpx;
    font-weight: 700;
    color: #3989FC;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      right: 0;
      left: 32rpx;
      display: block;
      height: 1px;
      background: #eee;
    }

    &::after {
      content: '';
      position: absolute;
      top: 40rpx;
      right: 32rpx;
      display: inline-block;
      width: 16rpx;
      height: 16rpx;
      border-top: 2rpx solid #999;
      border-right: 2rpx solid #999;
      transform: rotate(45deg);
    }
  }
</style>